package com.yict.csms.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yict.common.entity.PageEntity;
import com.yict.common.service.impl.CommonService;
import com.yict.common.util.DateUtil;
import com.yict.csms.system.dao.impl.HolidayDaoImpl;
import com.yict.csms.system.entity.Holiday;
import com.yict.csms.system.service.IHolidayService;

/**
 * 
 * 
 * @author ryan.wang
 * 
 */
@Service
public class HolidayServiceImpl extends CommonService<Holiday, Long> implements
		IHolidayService {
	@Autowired
	private HolidayDaoImpl holidayDaoImpl;

	public List<Holiday> search(Map<String, Object> queryMap, PageEntity page) {
		StringBuilder queryString = new StringBuilder();
		String field = " order by c.createDate desc";
		pageEntity = new PageEntity();
		queryString.append("from Holiday as c where 1=1");
		if (queryMap != null && queryMap.size() > 0) {
			if (queryMap.get("holidayName") != null && !"".equals(queryMap.get("holidayName"))) {
				queryString.append(" and lower(c.holidayName) like :holidayName");
				queryMap.put("holidayName", queryMap.get("holidayName").toString().toLowerCase());
			}
			if (queryMap.get("annualDate") != null && !"".equals(queryMap.get("annualDate"))) {
				queryString.append(" and (c.annual >= :annualDate and c.annual <= :annualEndDate or c.annual is null)");
				String endDate = (String)queryMap.get("annualDate") + "-12-31";
				queryMap.put("annualEndDate", DateUtil.StringToDate(endDate, 1));
				queryMap.put("annualDate", DateUtil.StringToDate((String)queryMap.get("annualDate"), 3));
			}
		}
		if (page.getField() != null&&!"".equals(page.getField()) &&page.getOrder()!= null && !"".equals(page.getOrder())) {
			field = " order by c." + page.getField() + page.getOrder();
		}
		queryString.append(field);

		List<Holiday> list = holidayDaoImpl.search(queryString.toString(), queryMap,
				page);
		pageEntity.setTotalRecord(holidayDaoImpl.getPageEntity().getTotalRecord());
		return list;
	}

	public LinkedHashMap<String, String> findAnnualMap() {
		List<Date> list =  holidayDaoImpl.findAnnual();
		LinkedHashMap<String, String> annualMap = new LinkedHashMap<String, String>();
		if (list != null) {
			for (Date annual : list) {
				String annu = DateUtil.DateToString(annual, 4);
				annualMap.put(annu, annu);
			}
		}
		return annualMap;
	}

	public boolean check(Date annual, Date beginDate, boolean begin, Long id) {
		boolean bool = false;
		bool = holidayDaoImpl.check(annual, beginDate, begin, id);
		return bool;
	}

	public boolean check(String holidayName, Long id) {
		boolean bool = false;
		bool = holidayDaoImpl.check(holidayName, id);
		return bool;
	}
	
	public Holiday findById(Long id) {
		return holidayDaoImpl.findById(id);
	}

	public Holiday check(Holiday holiday, boolean isSave, boolean isName) {
		List<Holiday> list = new ArrayList<Holiday>();
		StringBuilder queryString = new StringBuilder("from Holiday as c where 1=1 ");
		Map<String, Object> queryMap = new HashMap<String, Object>();
//		if(holiday.getAnnual() != null) {
//			queryString.append(" and c.annual = :annual");
//			queryMap.put("annual", holiday.getAnnual());
//		} 
		queryString.append(" and ((c.beginDate >=:beginDate and c.beginDate <=:endDate) " +
				"or (c.endDate >=:beginDate and c.endDate <=:endDate)) ");
		queryMap.put("beginDate", holiday.getBeginDate());
		queryMap.put("endDate", holiday.getEndDate());
		if (isName) {
			queryString.append(" and lower(c.holidayName) =:holidayName ");
			queryMap.put("holidayName", holiday.getHolidayName() == null ? null : holiday.getHolidayName().toLowerCase());
		}

		if(!isSave) {
			queryString.append(" and c.holidayId not in (:id)");
			queryMap.put("id", holiday.getHolidayId());
		}
		try {
			list = holidayDaoImpl.search(queryString.toString(), queryMap, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public Holiday check(String beginDate, String endDate, Long id) {
		List<Holiday> list = new ArrayList<Holiday>();
		StringBuilder queryString = new StringBuilder("from Holiday as c where 1=1 ");
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryString.append(" and TO_CHAR(c.beginDate,'yyyy-MM-dd') like :beginDate and TO_CHAR(c.endDate,'yyyy-MM-dd') like :endDate ");
		queryMap.put("beginDate", "%" + beginDate);
		queryMap.put("endDate", "%" + endDate);
		
		if(id != null) {
			queryString.append(" and c.holidayId not in (:id)");
			queryMap.put("id", id);
		}
		try {
			list = holidayDaoImpl.search(queryString.toString(), queryMap, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	/**
	 * 检查一天是否节假日
	 * @param date 
	 * @return 如果是节假日则为true，不是节假日为false
	 */
	@Override
	public boolean checkHoliday(Date date){
		String hql = "from Holiday h where h.beginDate <= :date and h.endDate >= :date";
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("date", date);
		List<Holiday> list = holidayDaoImpl.search(hql, map, null);
		
		return list.size() > 0;
	}
}
